<?php
namespace App\Api\Controllers\DevicePay;

use Illuminate\Support\Facades\DB;

class ScanpayController
{
    public function pay ($ways, $data_insert, $data, $store)
    {
        try {
            $company = explode('_', $ways->company);
            $dirName = '';
            foreach ($company as $c) {
                $dirName .= ucfirst($c);
            }
            if ($dirName == 'Cmbcunion') {
                $dirName = 'CmbcUnion';
            }
            if ($dirName == 'Lakalaunion') {
                $dirName = 'LakalaUnion';
            }
            if ($dirName == 'Leshuaunion') {
                $dirName = 'LeshuaUnion';
            }
            if (method_exists("App\Api\Controllers\\$dirName\LogicController",'payParams')) {
                $class = "\\App\\Api\\Controllers\\$dirName\\LogicController";
                if (isset($data['out_trade_no']) && !empty($data['out_trade_no'])) {
                    $out_trade_no = $data['out_trade_no'];
                } else {
                    $out_trade_no =  date('YmdHis', time()) . substr(microtime(), 2, 6) . sprintf('%03d', rand(0, 999));
                }
                $pay_type = $title = $ways_source_desc = $ways_type_desc = '';
                if ($ways->ways_source == 'weixin') {
                    $pay_type = 'WECHAT';
                    $title = '微信收款';
                    $ways_source_desc = '微信支付';
                    $ways_type_desc = '微信支付';
                }
                if ($ways->ways_source == 'alipay') {
                    $pay_type = 'ALIPAY';
                    $title = '支付宝收款';
                    $ways_source_desc = '支付宝';
                    $ways_type_desc = '支付宝';
                }
                if ($ways->ways_source == 'unionpay') {
                    $pay_type = 'UNIONPAY';
                    $title = '云闪付支付';
                    $ways_source_desc = '云闪付支付';
                    $ways_type_desc = '云闪付支付';
                }
                $data_public = [
                    'pay_type'         => $pay_type,
                    'return_params'    => '原样返回',
                    'title'            => $title,
                    'ways_source_desc' => $ways_source_desc,
                    'ways_type_desc'   => $ways_type_desc,
                    'code'             => isset($data['code']) ? $data['code'] : "",
                    'out_trade_no'     => $out_trade_no,
                    'other_no'         => isset($data['other_no']) ? $data['other_no'] : '',
                    'config_id'        => $store->config_id,
                    'store_id'         => isset($data['store_id']) ? $data['store_id'] : 0,
                    'store_pid'        => $store->pid,
                    'ways_type'        => '' . $ways->ways_type . '',
                    'ways_source'      => $ways->ways_source,
                    'company'          => $ways->company,
                    'total_amount'     => isset($data['total_amount']) ? $data['total_amount'] : 0,
                    'remark'           => isset($data['remark']) ? $data['remark'] : '',
                    'device_id'        => isset($data['device_id']) ? $data['device_id'] : '',
                    'device_type'      => isset($data['device_type']) ? $data['device_type'] : '',
                    'shop_name'        => $store->store_name,
                    'merchant_id'      => isset($data['merchant_id']) ? $data['merchant_id'] : "0",
                    'store_name'       => $store->store_name,
                    'tg_user_id'       => $store->user_id,
                    "couponDetail"     => isset($data['couponDetail']) ? $data['couponDetail'] : '',
                ];
                return (new $class)->payParams($data_insert, $data_public);
            } else {
                return json_encode([
                    'status' => 2,
                    'message' => '暂不支持此通道1'
                ]);
            }
        } catch (\Exception $e) {
            return json_encode([
                'status' => 2,
                'message' => '暂不支持此通道2'
            ]);
        }
    }

    public function order_query ($store_id, $store_pid, $data, $order)
    {
        try {
            $company = explode('_', $order->company);
            $dirName = '';
            foreach ($company as $c) {
                $dirName .= ucfirst($c);
            }
            if ($dirName == 'Cmbcunion') {
                $dirName = 'CmbcUnion';
            }
            if ($dirName == 'Lakalaunion') {
                $dirName = 'LakalaUnion';
            }
            if ($dirName == 'Leshuaunion') {
                $dirName = 'LeshuaUnion';
            }
            if (method_exists("App\Api\Controllers\\$dirName\LogicController",'queryParams')) {
                $class = "\\App\\Api\\Controllers\\$dirName\\LogicController";
                return (new $class)->queryParams($store_id, $store_pid, $data, $order);
            }
        } catch (\Exception $e) {
            return json_encode([
                'status' => 2,
                'message' => '暂不支持此通道2'
            ]);
        }
    }

    public function refund ($params, $order)
    {
        try {
            $company = DB::table('orders')->where('out_trade_no', $order->out_trade_no)->value('company');
            $company = explode('_', $company);
            $dirName = '';
            foreach ($company as $c) {
                $dirName .= ucfirst($c);
            }
            if ($dirName == 'Cmbcunion') {
                $dirName = 'CmbcUnion';
            }

            if ($dirName == 'Lakalaunion') {
                $dirName = 'LakalaUnion';
            }
            if ($dirName == 'Leshuaunion') {
                $dirName = 'LeshuaUnion';
            }
            if (method_exists("App\Api\Controllers\\$dirName\LogicController",'refundParams')) {
                $class = "\\App\\Api\\Controllers\\$dirName\\LogicController";
                return (new $class)->refundParams($params, $order);
            } else {
                return json_encode([
                    'status' => 2,
                    'message' => '退款失败',
                ]);
            }
        } catch (\Exception $e) {
            return json_encode([
                'status' => 2,
                'message' => '退款失败2'
            ]);
        }
    }

    public function refund_query ()
    {

    }

    public function qr ($pay_type, $StorePayWay)
    {
        $message = "您使用的客户端与要求不符~";
        return view('errors.page_errors', compact('message'));
    }

    public function qr_submit ($data_insert, $data)
    {
        try {
            $company = explode('_', $data_insert['company']);
            $dirName = '';
            foreach ($company as $c) {
                $dirName .= ucfirst($c);
            }
            if ($dirName == 'Cmbcunion') {
                $dirName = 'CmbcUnion';
            }

            if ($dirName == 'Lakalaunion') {
                $dirName = 'LakalaUnion';
            }
            if ($dirName == 'Leshuaunion') {
                $dirName = 'LeshuaUnion';
            }
            if (method_exists("App\Api\Controllers\\$dirName\LogicController",'qrSubmitParams')) {
                $class = "\\App\\Api\\Controllers\\$dirName\\LogicController";
                return (new $class)->qrSubmitParams($data_insert, $data);
            } else {
                return json_encode([
                    'status' => 2,
                    'message' => '暂不支持此通道'
                ]);
            }
        } catch (\Exception $e) {
            return json_encode([
                'status' => 2,
                'message' => '暂不支持此通道2'
            ]);
        }
    }

}
